package com.chuangyang.fixboxmaster.ui.fragment.orderprice;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.chuangyang.fixboxmaster.R;
import com.chuangyang.fixboxmaster.bean.CostItem;
import com.chuangyang.fixboxmaster.bean.CostItemInfos;
import com.chuangyang.fixboxmaster.service.FixBoxApi;
import com.chuangyang.fixboxmaster.service.GsonRequest;
import com.chuangyang.fixboxmaster.ui.ApplianceInfoActivity;
import com.chuangyang.fixboxmaster.ui.ModuleActivity;
import com.chuangyang.fixboxmaster.ui.fragment.BaseFragment;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

/**
 * Created by Administrator on 2015/6/11.
 */
public class DiagnosisFeeFragment extends BaseFragment implements View.OnClickListener{
    private View mContentView;

    private LinearLayout mDiagmosisFeeLayout;

    private TextView mAddPartView;

    private TextView mTitleView;

    private TextView mPriceView;

    private TextView mTotalPriceView;

    private TextView mOKView;

    private String mTransactionId;

    private String mSpecificationId;

    private String mProductDate;

    private String mOrderID;

    private String mBrandId;

    private ArrayList<CostItem> costItems = new ArrayList<>();
    public static final int REQUEST_CODE_REPAIR_TYPE = 1;
    private CostItem mSeletctItem;

    public static DiagnosisFeeFragment newInstance(){
        return new DiagnosisFeeFragment();
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mContentView = inflater.inflate(R.layout.fragment_diagnosis_fee,container,false);
        Bundle bundle = new Bundle();
        bundle = getArguments();
        mTransactionId = bundle.getString(ApplianceInfoActivity.TRANSCATION_ID);
        mSpecificationId = bundle.getString(ApplianceInfoActivity.SPECIFICATION_ID);
        mOrderID = bundle.getString(ApplianceInfoActivity.ORDER_ID);
        mProductDate = bundle.getString(ApplianceInfoActivity.PRODUCT_DATE);
        mBrandId = bundle.getString(ApplianceInfoActivity.BRAND_ID);
        initView();
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    private void initView(){
        mDiagmosisFeeLayout = (LinearLayout)mContentView.findViewById(R.id.diagnosis_fee_layout);
        mTitleView = (TextView)mContentView.findViewById(R.id.diagnosis_fee_title);
        mPriceView = (TextView)mContentView.findViewById(R.id.diagnosis_fee_price);
        mAddPartView = (TextView)mContentView.findViewById(R.id.add_part);
        mTotalPriceView = (TextView)mContentView.findViewById(R.id.total_cost);
        mOKView = (TextView)mContentView.findViewById(R.id.btn_ok);
        mAddPartView.setOnClickListener(this);
        mOKView.setOnClickListener(this);
        mDiagmosisFeeLayout.setOnClickListener(this);
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setContentView(mContentView);
        loadData();
    }

    private void loadData(){
        HashMap<String, String> param = new HashMap<>();
        param.put("areaId", ""+1);
        param.put("transactionId", mTransactionId);
        param.put("specificationId", mSpecificationId);
        param.put("id",mOrderID);
        addRequest(new GsonRequest<>(FixBoxApi.GET_DIAGNOSIS_FREE, param, CostItemInfos.class, responseListener));

    }


    private Response.Listener<CostItemInfos> responseListener = new Response.Listener<CostItemInfos>() {
        @Override
        public void onResponse(CostItemInfos response) {

            if (response.result != null)
            {
                costItems.clear();
                costItems.addAll(Arrays.asList(response.result));
                for (CostItem item:costItems)
                {
                    if (item.type == 4)
                    {
                        setCurrentType(item);
                    }
                }
            }

            showResult();
        }
    };

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode)
        {
            case  REQUEST_CODE_REPAIR_TYPE:

                if (data != null)
                {

                    Bundle bundle = data.getExtras();
                    if (bundle != null)
                    {
                        CostItem item = (CostItem)bundle.getSerializable("cost_item");
                        if (item != null)
                        {
                            setCurrentType(item);
                        }
                    }
                }
                break;
        }
    }

    private void setCurrentType(CostItem item){

        if (item != null)
        {
            if (item.type == 4)
            {
                mTitleView.setText(item.title);
            }
            else
            {
                mTitleView.setText("维修费");
            }
            mPriceView.setText("￥"+item.price);
            mTotalPriceView.setText("合计: ￥" + item.price);
            mSeletctItem = item;
        }
    }

    private CostItem getCostItem(){
        CostItem item = new CostItem();

        if (mSeletctItem != null)
        {
            item.price = mSeletctItem.price;
            item.num = mSeletctItem.num;

            if (item.num > 0)
            {
                if (mSeletctItem.type == 4)//上门诊断费
                {
                    item.type = 1;
                    item.partId = 0;
                    item.title = mSeletctItem.title;
                }
                else
                {
                    item.type = 2;
                    item.partId = 0;
                    item.title = "维修费";
                }
            }
        }
        return item;
    }

    @Override
    public void onClick(View v) {

        switch (v.getId())
        {
            case R.id.diagnosis_fee_layout:
                Intent repairTypeIntent = new Intent(getActivity(), ModuleActivity.class);
                repairTypeIntent.putExtra(ModuleActivity.TYPE, ModuleActivity.REPAIR_TYPE_ACTIVITY);
                repairTypeIntent.putExtra(ModuleActivity.REPAIR_TYPE, mSeletctItem);
                repairTypeIntent.putExtra(ModuleActivity.COST_ITEM, costItems);
                startActivityForResult(repairTypeIntent,REQUEST_CODE_REPAIR_TYPE);
                break;
            case R.id.add_part:
                if (mSeletctItem != null && mSeletctItem.type != 4)
                {
                    Intent partIntent = new Intent(getActivity(), ApplianceInfoActivity.class);
                    partIntent.putExtra(ApplianceInfoActivity.TYPE, ApplianceInfoActivity.APPLIANCE_PART_ACTIVITY);
                    partIntent.putExtra(ApplianceInfoActivity.SPECIFICATION_ID,""+mSpecificationId);
                    partIntent.putExtra(ApplianceInfoActivity.TRANSCATION_ID,""+mTransactionId);
                    partIntent.putExtra(ApplianceInfoActivity.ORDER_ID,""+mOrderID);
                    partIntent.putExtra(ApplianceInfoActivity.PRODUCT_DATE,""+mProductDate);
                    partIntent.putExtra(ModuleActivity.BRAND_ID, mBrandId);
                    partIntent.putExtra(ModuleActivity.REPAIR_TYPE, mSeletctItem);
                    partIntent.putExtra(ModuleActivity.COST_ITEM, costItems);
                    startActivity(partIntent);
                }
                else
                {
                    Toast.makeText(getActivity(),"请选择维修类别!",Toast.LENGTH_LONG).show();
                }
                break;
            case R.id.btn_ok:
                Intent costDetailIntent = new Intent(getActivity(), ModuleActivity.class);
                costDetailIntent.putExtra(ModuleActivity.TYPE, ModuleActivity.COST_DETAIL_ACTIVITY);
                ArrayList<CostItem> items = new ArrayList<>();
                items.add(getCostItem());
                costDetailIntent.putExtra(ModuleActivity.COST_ITEM, items);
                costDetailIntent.putExtra(ModuleActivity.ORDER_ID, mOrderID);
                costDetailIntent.putExtra(ApplianceInfoActivity.SPECIFICATION_ID,""+mSpecificationId);
                costDetailIntent.putExtra(ApplianceInfoActivity.TRANSCATION_ID,""+mTransactionId);
                costDetailIntent.putExtra(ApplianceInfoActivity.PRODUCT_DATE,""+mProductDate);
                costDetailIntent.putExtra(ModuleActivity.BRAND_ID, mBrandId);
                startActivity(costDetailIntent);
                break;
        }

    }
}
